package com.ruoyi.common.utils.sql;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class InsertSqlUtil {

    public static String buildInsertSql(String measurement, Set<String> tagKeys, Set<String> fieldKeys) {
        StringBuilder sql = new StringBuilder("INSERT INTO ").append(measurement).append(" (");
        StringBuilder placeholders = new StringBuilder();

        // Append tag keys and placeholders
        for (String key : tagKeys) {
            sql.append(key).append(", ");
            placeholders.append("?, ");
        }

        // Append field keys and placeholders
        for (String key : fieldKeys) {
            sql.append(key).append(", ");
            placeholders.append("?, ");
        }

        // Remove trailing comma and space
        sql.setLength(sql.length() - 2);
        placeholders.setLength(placeholders.length() - 2);

        sql.append(") VALUES (").append(placeholders).append(")");
        return sql.toString();
    }

    public static String BuildQueryParameter(Map<String, Object> params,Map<String, List<String>> measurementDictMap){
        String from = (String) params.get("from");
        String to = (String) params.get("to");
        String remoteId = (String) params.getOrDefault("remoteId", null);
        String time = (String) params.getOrDefault("time", "1d");
        String limit = (String) params.getOrDefault("limit", "32");
        String measurement = (String) params.getOrDefault("measurement", "");
//        Map<String, List<String>> measurementDictMap = getMeasurementDictMap(measurement);
        List<String> fields = measurementDictMap.get("field");
        String firstFieldsClause = buildFirstFieldsClause(fields);
        String SQL = "SELECT " + firstFieldsClause + " FROM \"" + measurement + "\"";
        SQL = SQL.concat(" WHERE ");
        if( !ObjectUtils.isEmpty(from) && !ObjectUtils.isEmpty(to)) {
            SQL = SQL.concat(  "time >= " + from + "ms and time <= " + to + "ms AND ");
        }
        if (!ObjectUtils.isEmpty(remoteId)) {
            SQL = SQL.concat("  (\"remoteId\" = \'" +  remoteId + "\') ");
        }
        return SQL.concat(" GROUP BY time(" + time + ")  ")
            .concat(" ORDER BY time DESC")
            .concat(" LIMIT " + limit);
    }

    public static String buildFirstFieldsClause(List<String> fields) {
        return fields.stream()
            .map(field -> "last(\"" + field + "\") as " + field + " ")
            .collect(Collectors.joining(", "));
    }

//    public Map<String, List<String>> getMeasurementDictMap(String measurement) {
//        Map<String, List<String>> measurementDictMap = new HashMap<>();
//        measurementDictMap.putAll(iotMeasurementDictMapper.selectList(new LambdaQueryWrapper<IotMeasurementDict>().eq(IotMeasurementDict::getMeasurement, measurement))
//            .stream()
//            .collect(Collectors.groupingBy(IotMeasurementDict::getType,
//                Collectors.mapping(IotMeasurementDict::getName, Collectors.toList()))));
//        return measurementDictMap;
//    }
}
